/**
 * Fw.Security
 */
Fw.define(function(require){
	//core
	require('security/core');
    //MD5
    require('security/md5');
    //AES
    require('security/aes');
    //RSA
    require('security/rsa');
    
    //
    Fw.Security = {
        /**
         * 默认的加密方式
         */
        encrypt: function(){
            //默认使用三段式加密
            return Fw.Security.TripleDES.encrypt.apply(this, arguments);
        },
        /**
         * MD5
         */
        MD5: {
            /**
             * 摘要
             * @param {String} data
             */
            digest: function(data){
                return CryptoJS.MD5(data);
            }
        },
        /**
         * AES
         */
        AES: {
            /**
             * 加密
             * @param {String} data
             * @param {String} key
             * @param {String} iv
             * @param {String} mode
             * @param {String} pad
             */
            encrypt: function(data, key, iv, mode, pad){
                //
                key = CryptoJS.enc.Utf8.parse(key || Fw.Security.AES.getKey());
                iv = iv || key;
                mode = mode || CryptoJS.mode.CBC;
                pad = pad || CryptoJS.pad.Pkcs7;
                
                //
                data = CryptoJS.AES.encrypt(data, key, {
                    iv: iv,
                    mode: mode,
                    padding: pad
                });
                
                //
                return data.ciphertext.toString(CryptoJS.enc.Base64);
            },
            /**
             * 返回AES密钥
             */
            getKey: function(){
                //TODO...
                return '';
            }
        },
        /**
         * RSA
         */
        RSA: {
            /**
             * 加密
             * @param {String} data
             * @param {String} key
             */
            encrypt: function(data, key){
                var pubkey = pem_import(key || Fw.Security.RSA.getKey());
                data = pubkey.encrypt(data);
                return Base64.hex2b64(data);
            },
            /**
             * 返回RSA密钥
             */
            getKey: function(){
                //TODO...
                return '';
            }
        },
        /**
         * 三段式加密
         */
        TripleDES: {
            /**
             * 加密
             * @param {String} data
             * @param {String} key
             */
            encrypt: function(data, key){
                var aesKey = Fw.uuid(16);
                var md5Digest = Fw.Security.MD5.digest(aesKey + data);
                var rsaText = Fw.Security.RSA.encrypt(aesKey, key || Fw.Security.TripleDES.getKey());
                var aesText = Fw.Security.AES.encrypt(data, aesKey);
                var result = [md5Digest, aesText, rsaText];
                return result.join(String.fromCharCode(29));
            },
            /**
             * 返回TripleDES/RSA密钥
             */
            getKey: function(){
                //TODO...
                return 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8YLzn7q9cB5it9P418SFoyo7iqi3y/jN229Oey/IrK2MjeGvzbg6O0z3M/HMq/dCtZ0ZBtRMRES+sdQkzKpCO9fE744McQimK1zB585FcHCGTb3EMPsHLzT4+lv9eJ1XFn2mwoQiLY5YRjU0Mfqp4Y1bgrYs8a4N2iI08XCK0DwIDAQAB';
            }
        }
    };
});
